package com.mxpio.erp.workshop.provider;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.WorkShopEnums;
import com.mxpio.erp.common.technology.entity.WorkShop;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criterion;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.jpa.query.SimpleCriterion;
import com.mxpioframework.security.access.datascope.provider.DataScapeProvider;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author : wpp
 * @Datetime : 2023/4/19 15:33
 * @Desc :
 * @Modor :  Modifytime:
 * @modDesc : 试制车间生产订单过滤器
 */
@Component
public class TrialMoProvider implements DataScapeProvider {
    @Override
    @Transactional
    public List<Criterion> provide() {
        List<Criterion> appendCriterions = new ArrayList<>();
        List<WorkShop> workShops = JpaUtil.linq(WorkShop.class).equal("status", CommonEnums.YesNo.YES.getCode())
                .equal("workShopType", WorkShopEnums.WorkShopType.TRIAL.getCode()).list();
        if (workShops.size()>0){
            Set<String> codeSet = workShops.stream().map(WorkShop::getWorkShopCode).collect(Collectors.toSet());
            appendCriterions.add(new SimpleCriterion("mainWorkshop", Operator.IN,codeSet));
        }else{
            appendCriterions.add(new SimpleCriterion("createTime", Operator.EQ,new Date(0)));//如果未匹配到过滤条件，则增加一个不能正常匹配到数据的条件
        }
        return appendCriterions;
    }
}
